<template>
  <a-modal
    :title="title"
    :width="780"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    :destroyOnClose="true"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="表单类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select v-decorator="['form_type', { rules: [{ required: true, message: '请选择表单类型' }] }]">
            <a-select-option v-for="(item, index) in formTypeList" :key="index" :value="item.value">{{
              item.name
            }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="所属用户" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select v-decorator="['user_id', { rules: [{ required: true, message: '请选择用户' }] }]">
            <a-select-option v-for="(item, index) in userList" :key="index" :value="item.user_id">{{
              item.nick_name
            }}</a-select-option>
          </a-select>
          <div class="form-item-help">
            <router-link target="_blank" :to="{ path: '/subscriber/index' }">去新增</router-link>
            <a href="javascript:;" @click="onReloadUserList">刷新</a>
          </div>
        </a-form-item>
        <a-form-item label="手机号码" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number
            :min="0"
            v-decorator="['phone', { rules: [{ required: true, message: '请输入至少1个数字' }] }]"
          />
        </a-form-item>
        <a-form-item label="地址" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['address', { rules: [{ min: 2, message: '请输入至少2个字符' }] }]" />
        </a-form-item>
        <a-form-item label="品牌车型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['brand', { rules: [{ min: 2, message: '请输入至少2个字符' }] }]" />
        </a-form-item>
        <a-form-item label="行驶里程" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-radio-group
            v-decorator="['mileage', { initialValue: '无', rules: [{ required: true, message: '请选择行驶里程' }] }]"
          >
            <a-radio value="无">无</a-radio>
            <a-radio value="1-3万">1-3万</a-radio>
            <a-radio value="3-5万">3-5万</a-radio>
            <a-radio value="5-10万">5-10万</a-radio>
            <a-radio value="10万以上">10万以上</a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item label="车辆种类" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select v-decorator="['car_type_id', { rules: [{ required: true, message: '请选择车辆种类' }] }]">
            <a-select-option v-for="(item, index) in carTypeList" :key="index" :value="item.car_type_id">{{
              item.name
            }}</a-select-option>
          </a-select>
          <div class="form-item-help">
            <router-link target="_blank" :to="{ path: '/appointment/carType' }">去新增</router-link>
            <a href="javascript:;" @click="onReloadCarTypeList">刷新</a>
          </div>
        </a-form-item>
        <a-form-item label="预约类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select v-decorator="['appointment_type', { rules: [{ required: true, message: '请选择预约类型' }] }]">
            <a-select-option v-for="(item, index) in appointmentTypeList" :key="index" :value="item.value">{{
              item.name
            }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="预约时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-date-picker v-decorator="['time', { rules: [{ required: true, message: '请选择预约时间' }] }]" showTime />
        </a-form-item>
        <a-form-item label="状态" :labelCol="labelCol" :wrapperCol="{span: 6}">
          <a-select v-decorator="['status', { rules: [{ required: true, message: '请选择预约类型' }] }]">
            <a-select-option v-for="(item) in statusList" :key="item.value" :value="item.value">{{
              item.name
            }}</a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import moment from 'moment'
import pick from 'lodash.pick'
import * as Api from '@/api/appointment/index'
import * as UserApi from '@/api/appointment/user'
import * as CarTypeApi from '@/api/appointment/carType'
import AppointmentTypeEnum from '@/common/enum/appointment/AppointmentType'
import FormTypeEnum from '@/common/enum/appointment/FormType'
import FormStatusEnum from '@/common/enum/appointment/FormStatus'

export default {
  data() {
    return {
      // 对话框标题
      title: '新增预约',
      // 标签布局属性
      labelCol: {
        span: 7,
      },
      // 输入框布局属性
      wrapperCol: {
        span: 13,
      },
      // modal(对话框)是否可见
      visible: false,
      // modal(对话框)确定按钮 loading
      confirmLoading: false,
      // 当前表单元素
      form: this.$form.createForm(this),
      // 车辆种类列表
      carTypeList: [],
      // 用户列表
      userList: [],
    }
  },
  computed: {
    formTypeList() {
      return FormTypeEnum.getData()
    },

    appointmentTypeList() {
      return AppointmentTypeEnum.getData()
    },

    statusList() {
      return FormStatusEnum.getData()
    }
  },
  methods: {
    /**
     * 显示对话框
     */
    add() {
      // 显示窗口
      this.visible = true
      // 获取用户列表
      this.getUserList()
      // 获取车辆种类列表
      this.getCarTypeList()
    },

    // 获取用户列表
    getUserList() {
      return new Promise((resolve, reject) => {
        UserApi.all()
          .then((res) => {
            this.userList = res.data.list
          })
          .finally(() => {
            resolve()
          })
      })
    },

    // 获取车辆种类列表
    getCarTypeList() {
      return new Promise((resolve, reject) => {
        CarTypeApi.list()
          .then((res) => {
            console.log(res)
            this.carTypeList = res.data.list
          })
          .finally(() => {
            resolve()
          })
      })
    },

    onReloadCarTypeList() {
      this.isLoading = true
      this.getCarTypeList().then(() => {
        this.isLoading = false
      })
    },

    /**
     * 确认按钮
     */
    handleSubmit(e) {
      e.preventDefault()
      // 表单验证
      const {
        form: { validateFields },
      } = this
      validateFields((errors, values) => {
        // 提交到后端api

        !errors && this.onFormSubmit(values)
      })
    },

    /**
     * 关闭对话框事件
     */
    handleCancel() {
      this.visible = false
      this.form.resetFields()
    },

    // 刷新用户列表
    onReloadUserList() {
      this.isLoading = true
      this.getUserList().then(() => {
        this.isLoading = false
      })
    },

    /**
     * 提交到后端api
     */
    onFormSubmit(values) {
      this.confirmLoading = true
      values.time = moment(values.time).format('YYYY-MM-DD HH:mm:ss')
      Api.add({ form: values })
        .then((result) => {
          // 显示成功
          this.$message.success(result.message, 1.5)
          // 关闭对话框事件
          this.handleCancel()
          // 通知父端组件提交完成了
          this.$emit('handleSubmit', values)
        })
        .finally((result) => {
          this.confirmLoading = false
        })
    },
  },
}
</script>
<style scoped>
.ant-input-number {
  width: 200px !important;
}
</style>